package com.bsoft.gol.hcs.specialist.bean.vo;

import com.bsoft.gol.hcs.specialist.bean.request.DiseaseFollowUpStatisticsReq;
import com.bsoft.gol.hcs.specialist.bean.request.WorkloadStatisticsReq;
import com.bsoft.gol.hcs.specialist.bean.response.DiseaseFollowUpStatisticsResp;
import com.bsoft.gol.hcs.specialist.bean.response.WorkloadStatisticsResp;
import com.bsoft.gol.hcs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

;


public class FollowUpStatisticExcelUtil {

    private final static Logger logger = LoggerFactory.getLogger(FollowUpStatisticExcelUtil.class);

    private static CellStyle style;

    /**
     * 生成病种随访统计
     *
     * @throws NullPointerException
     */
    public static byte[] getExcelAsByteArrayForDiseaseFollowUpStatistics(DiseaseFollowUpStatisticsReq qo, List<DiseaseFollowUpStatisticsResp> dataList) throws ControllerException {
        List<String> titleList = new ArrayList<>();
        titleList.add("机构");
        titleList.add("科室");
        titleList.add("病种");
        titleList.add("患者总数");
        titleList.add("当前在组人数");
        titleList.add("成功随访次数");
        titleList.add("复诊率");
        titleList.add("失访人数");

        Workbook wb;
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        try {
            wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet("病种随访统计");

            initCellStyle(wb);
            initHeadStyle(sheet, titleList);
            initHeadContentForDis(titleList, sheet);
            initBodyForDis(dataList, sheet);

            wb.write(out);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return out.toByteArray();
    }

    /**
     * 生成随访工作量统计
     *
     * @throws NullPointerException
     */
    public static byte[] getExcelAsByteArrayForWorkloadStatistics(WorkloadStatisticsReq qo, List<WorkloadStatisticsResp> dataList) throws ControllerException {
        if (Objects.equals(qo.getStatisticsType(),"0")){
            List<String> titleList = new ArrayList<>();
            titleList.add("团队");
            titleList.add("随访人次");
            titleList.add("随访成功人次");
            titleList.add("逾期次数");
            titleList.add("逾期次数占比(%)");
            titleList.add("复诊次数");
            titleList.add("邀约患者人数");


            Workbook wb;
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            try {
                wb = new XSSFWorkbook();
                Sheet sheet = wb.createSheet("随访工作量统计");

                initCellStyle(wb);
                initHeadStyle(sheet, titleList);
                initHeadContentForWork(titleList, sheet);
                initBodyForWork(dataList, sheet, false);

                wb.write(out);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            return out.toByteArray();
        }else {
            List<String> titleList = new ArrayList<>();
            titleList.add("团队");
            titleList.add("工号");
            titleList.add("姓名");
            titleList.add("随访人次");
            titleList.add("随访成功人次");
            titleList.add("逾期次数");
            titleList.add("逾期次数占比(%)");
            titleList.add("复诊次数");
            titleList.add("邀约患者人数");


            Workbook wb;
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            try {
                wb = new XSSFWorkbook();
                Sheet sheet = wb.createSheet("随访工作量统计");

                initCellStyle(wb);
                initHeadStyle(sheet, titleList);
                initHeadContentForWork(titleList, sheet);
                initBodyForWork(dataList, sheet,true);

                wb.write(out);
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
            return out.toByteArray();
        }
    }

    private static void initBodyForDis(List<DiseaseFollowUpStatisticsResp> dataList, Sheet sheet) {
        if (CommonUtils.isNotEmpty(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                Row row = sheet.createRow(i + 3);
                DiseaseFollowUpStatisticsResp resp = dataList.get(i);
                setCellValue(row, 0, style, resp.getOrgName());
                setCellValue(row, 1, style, resp.getDeptName());
                setCellValue(row, 2, style, resp.getChronicDiseaseName());
                setCellValue(row, 3, style, CommonUtils.isEmpty(resp.getPatientsNum()) ? "" : resp.getPatientsNum()+"");
                setCellValue(row, 4, style, CommonUtils.isEmpty(resp.getInGroupNum()) ? "" : resp.getInGroupNum()+"");
                setCellValue(row, 5, style, CommonUtils.isEmpty(resp.getSucFollowUpNum()) ? "" : resp.getSucFollowUpNum()+"");
                setCellValue(row, 6, style, CommonUtils.isEmpty(resp.getFollowUpRate()) ? "" : resp.getFollowUpRate());
                setCellValue(row, 7, style, CommonUtils.isEmpty(resp.getLostVisitNum()) ? "" : resp.getLostVisitNum()+"");
            }
        }
    }

    private static void initBodyForWork(List<WorkloadStatisticsResp> dataList, Sheet sheet,boolean flag) {
        if (CommonUtils.isNotEmpty(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                Row row = sheet.createRow(i + 3);
                WorkloadStatisticsResp resp = dataList.get(i);
                if (flag){
                    setCellValue(row, 0, style, resp.getTeamName());
                    setCellValue(row, 1, style, resp.getDocCode());
                    setCellValue(row, 2, style, resp.getDocName());
                    setCellValue(row, 3, style, CommonUtils.isEmpty(resp.getFollowUpNum()) ? "" : resp.getFollowUpNum()+"");
                    setCellValue(row, 4, style, CommonUtils.isEmpty(resp.getSucFollowUpNum()) ? "" : resp.getSucFollowUpNum()+"");
                    setCellValue(row, 5, style, CommonUtils.isEmpty(resp.getBeOverdueNum()) ? "" : resp.getBeOverdueNum()+"");
                    setCellValue(row, 6, style, CommonUtils.isEmpty(resp.getBeOverdueRate()) ? "" : resp.getBeOverdueRate());
                    setCellValue(row, 7, style, CommonUtils.isEmpty(resp.getFollowUpVisitNum()) ? "" : resp.getFollowUpVisitNum()+"");
                    setCellValue(row, 8, style, CommonUtils.isEmpty(resp.getEnterGroupNum()) ? "" : resp.getEnterGroupNum()+"");
                }else {
                    setCellValue(row, 0, style, resp.getTeamName());
                    setCellValue(row, 1, style, CommonUtils.isEmpty(resp.getFollowUpNum()) ? "" : resp.getFollowUpNum()+"");
                    setCellValue(row, 2, style, CommonUtils.isEmpty(resp.getSucFollowUpNum()) ? "" : resp.getSucFollowUpNum()+"");
                    setCellValue(row, 3, style, CommonUtils.isEmpty(resp.getBeOverdueNum()) ? "" : resp.getBeOverdueNum()+"");
                    setCellValue(row, 4, style, CommonUtils.isEmpty(resp.getBeOverdueRate()) ? "" : resp.getBeOverdueRate());
                    setCellValue(row, 5, style, CommonUtils.isEmpty(resp.getFollowUpVisitNum()) ? "" : resp.getFollowUpVisitNum()+"");
                    setCellValue(row, 6, style, CommonUtils.isEmpty(resp.getEnterGroupNum()) ? "" : resp.getEnterGroupNum()+"");
                }
            }
        }

    }

    private static void initHeadContentForWork(List<String> titleList, Sheet sheet) {
        Row row0 = sheet.createRow(0);
        Row row2 = sheet.createRow(2);
        Row row3 = sheet.createRow(3);
        String title = "随访工作量统计表";

        setCellValue(row0, 0, style, title);

        for (int i = 0; i < titleList.size(); i++) {
            String headerName = titleList.get(i);
            setCellValue(row2, i, style, headerName);
        }
    }

    private static void initHeadContentForDis(List<String> titleList, Sheet sheet) {
        Row row0 = sheet.createRow(0);
        Row row2 = sheet.createRow(2);
        Row row3 = sheet.createRow(3);
        String title = "病种随访统计表";

        setCellValue(row0, 0, style, title);

        for (int i = 0; i < titleList.size(); i++) {
            String headerName = titleList.get(i);
            setCellValue(row2, i, style, headerName);
        }
    }

    private static void initHeadStyle(Sheet sheet, List<String> titleList) {
        sheet.setColumnWidth(0, 15 * 256); // 设置宽度

        // 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, titleList.size() - 1));

    }

    private static void setCellValue(Row row, int column, CellStyle style, String value) {
        Cell cell = row.createCell(column);
        cell.setCellStyle(style);
        cell.setCellValue(value);
    }

    private static void initCellStyle(Workbook wk) {
        style = wk.createCellStyle();
//        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平
        style.setWrapText(true); //自动换行
    }


}
